<script setup>
import axios from 'axios'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import router from '../router/router'
const route = useRoute()
const user = ref({})
const query = (username) => {
  axios.get('api/usercontroller/person/' + username).then((res) => {
    console.log(res.data)
    user.value = res.data
  })
}
const back = () => {
  router.push('/dynamicSpace')
}

const isFollowing = ref(false)
const buttonText = ref('关注')
const isFollowed = (username) => {
  axios.get('api/follow/or/not/' + username).then((res) => {
    isFollowing.value = res.data
    // 处理按钮点击事件的逻辑
    if (!isFollowing.value) {
      // 取消关注的操作

      buttonText.value = '关注'
      // 执行后端取消关注的请求
    } else {
      // 关注的操作
      buttonText.value = '已关注'
      // 执行后端关注的请求
    }
  })
}
const toggleFollow = () => {
  axios.get('api/follow/' + user.value.username + '/' + isFollowing.value).then((res) => {
    isFollowing.value = res.data
    // 处理按钮点击事件的逻辑
    if (!isFollowing.value) {
      // 取消关注的操作
      isFollowing.value = false
      buttonText.value = '关注'
      // 执行后端取消关注的请求
    } else {
      // 关注的操作
      isFollowing.value = true
      buttonText.value = '已关注'
      // 执行后端关注的请求
    }
  })
}
onMounted(() => {
  const username = route.query.username
  query(username)
  isFollowed()
})
</script>

<template>
  <div class="user-page">
    <span style=" font-size: 24px;" @click="back()"><el-icon>
        <Back />
      </el-icon></span>
    <div class="user-profile">
      <div class="avatar">
        <img :src="'src/' + user.avatarUrl" :alt="user.name" class="avatar-img" />
      </div>
      <div class="foot">
        <div class="user-info">
          <h2>{{ user.username }}</h2>
          <p class="signature">{{ user.introduction }}</p>
        </div>

        <div class="logout-btn" @click="toggleFollow">
          {{ isFollowing ? '已关注' : '关注' }}
        </div>

      </div>
    </div>
  </div>
</template>

<style scoped>
.logout-btn {
  margin-left: 10px;
  background-color: rgb(127, 214, 214);
  font-size: 20px;
  justify-content: center;
  align-self: center;
}

.foot {
  display: flex;
}

.user-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: #f2f2f2;
  /* 设置背景颜色 */
}

.back-btn {
  position: absolute;
  top: 20px;
  left: 20px;
  cursor: pointer;
  font-size: 24px;
  color: #0f0c0c;
}

.user-profile {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 20px;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.avatar-img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
}

.user-info {
  text-align: center;
  margin-top: 10px;
}

.signature {
  font-style: italic;
  color: gray;
}
</style>
